import {
  ClassicModel
} from "../../models/classic.js";
import {
  LikeModel
} from "../../models/like.js";

let classicModel = new ClassicModel();
let likeModel = new LikeModel();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    classic: null,
    latest: true,
    first: false,
    likeCount: 0,
    likeStatus: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    classicModel.getLatest((res) => {
      this.setData({
        classic: res,
        likeCount: res.fav_nums,
        likeStatus: res.like_status
      })
    });
  },

  onLikeTap: function(event) {
    const like_status = event.detail.behavior;
    likeModel.like(like_status, this.data.classic.id, this.data.classic.type);
  },

  onNext: function(event) {
    this._updateClassic("next");
  },

  onPrevious: function(event) {
    this._updateClassic("previous");
  },

  _updateClassic: function(nextOrPrevious) {
    let index = this.data.classic.index;
    classicModel.getClassic(index, nextOrPrevious, (res) => {
      this._getLikeStatus(res.id, res.type);
      let first = classicModel.isFirst(res.index);
      let latest = classicModel.isLatest(res.index);
      this.setData({
        classic: res,
        first: first,
        latest: latest
      })
    })
  },

  _getLikeStatus: function(artID, category) {
    likeModel.getClassicLikeStatus(artID, category, (res) => {
      this.setData({
        likeCount: res.data.fav_nums,
        likeStatus: res.data.like_status
      })
    })
  },

  onShareAppMessage: function() {
    return {
      title: this.data.classic.title,
    }
  },

})